package com.prd.controller;


import com.prd.dto.SysUserDTO;
import com.prd.dto.SysRoleDTO;
import com.prd.dto.SysUserRoleDTO;
import com.prd.service.SysRoleService;
import com.prd.service.SysUserService;
import com.prd.util.JsonData;
import com.prd.web.controller.BaseController;
import com.prd.web.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 角色数据表 前端控制器
 * </p>
 *
 * @author lizhaoyi
 * @since 2024-01-09
 */
@RestController
@RequestMapping("/api/sysRole")
@Api(tags = "角色管理")
public class SysRoleController extends BaseController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysUserService sysUserService;

    @ApiOperation("新增角色")
    @PostMapping("addRole")
    public JsonData add(@Validated @RequestBody SysRoleDTO roleDTO) {

        return sysRoleService.insertRole(roleDTO);
    }

    @ApiOperation("修改保存角色")
    @PutMapping("edit")
    public JsonData edit(@Validated @RequestBody SysRoleDTO roleDTO) {

        return sysRoleService.updateRole(roleDTO);
    }

    @ApiOperation("删除角色")
    @DeleteMapping("/{roleIds}")
    public JsonData remove(@PathVariable Long[] roleIds) {
        return sysRoleService.deleteRoleByIds(roleIds);
    }


    @ApiOperation("获取角色列表")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody SysRoleDTO sysRoleDTO) {

        startPage();
        List<SysRoleDTO> list = sysRoleService.selectRoleList(sysRoleDTO);
        return getDataTable(list);
    }

    @ApiOperation("根据角色id获取详细信息")
    @GetMapping(value = "/{roleId}")
    public JsonData getInfo(@PathVariable Long roleId) {

        return sysRoleService.selectRoleById(roleId);
    }

    @ApiOperation("查询已分配用户角色列表")
    @PostMapping("/user/allocatedList")
    public TableDataInfo allocatedList(@RequestBody SysUserDTO userDTO) {
        startPage();
        List<SysUserDTO> list = sysUserService.selectAllocatedList(userDTO);
        return getDataTable(list);
    }

    @ApiOperation("查询未分配用户角色列表")
    @PostMapping("/user/unallocatedList")
    public TableDataInfo unallocatedList(@RequestBody SysUserDTO userDTO) {
        startPage();
        List<SysUserDTO> list = sysUserService.selectUnallocatedList(userDTO);
        return getDataTable(list);
    }

    @ApiOperation("取消授权用户")
    @PutMapping("/user/cancel")
    public JsonData cancelAuthUser(@RequestBody SysUserRoleDTO userRoleDTO) {

        return sysRoleService.deleteAuthUser(userRoleDTO);
    }

    @ApiOperation("批量取消授权用户")
    @PutMapping("/user/cancelAll")
    public JsonData cancelAuthUserAll(Long roleId, Long[] userIds) {

        return sysRoleService.deleteAuthUsers(roleId, userIds);
    }

    @ApiOperation("批量选择用户授权")
    @PutMapping("/user/selectAll")
    public JsonData selectAuthUserAll(Long roleId, Long[] userIds) {

        return sysRoleService.insertAuthUsers(roleId, userIds);
    }

}

